"""
考勤记录模型
"""
from datetime import datetime
from app import db


class AttendanceRecord(db.Model):
    """考勤记录表"""
    __tablename__ = 'mx_attendance_records'
    
    id = db.Column(db.Integer, primary_key=True)
    employee_id = db.Column(db.String(50), db.ForeignKey('mx_employees.employee_id'), nullable=False)
    record_date = db.Column(db.Date, nullable=False, comment='考勤日期')
    record_type = db.Column(db.String(20), nullable=False, comment='类型: leave, overtime, normal')
    hours = db.Column(db.Float, comment='时长（小时）')
    remark = db.Column(db.String(500), comment='备注')
    application_id = db.Column(db.Integer, db.ForeignKey('mx_leave_applications.id'), comment='关联申请ID')
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    # 关联
    employee = db.relationship('Employee', backref='attendance_records')
    application = db.relationship('LeaveApplication', backref='attendance_records')
    
    def to_dict(self):
        return {
            'id': self.id,
            'employee_id': self.employee_id,
            'employee_name': self.employee.name if self.employee else None,
            'record_date': self.record_date.isoformat() if self.record_date else None,
            'record_type': self.record_type,
            'hours': self.hours,
            'remark': self.remark,
            'application_id': self.application_id,
            'created_at': self.created_at.isoformat() if self.created_at else None
        }
